﻿Imports System.Xml
Imports System.Xml.Xsl
Imports BusinessLogic
Imports System.Net.Mail

Partial Class TopTen
    Inherits System.Web.UI.Page
    Public total As String = "0"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BindPrizes()
        If Session("Username") IsNot Nothing Then
            Dim _Points As New Points()
            _Points.Username = Session("Username").ToString()
            _Points.LoadPointsByUserName()
            litUsername.Text = Session("Username").ToString()
            litTotalPoints.Text = _Points.TotalPoints
            total = _Points.TotalPoints
        End If
    End Sub
    Private Sub BindPrizes()
        Dim oXmlDocument As New XmlDocument()
        oXmlDocument.Load(Server.MapPath("~/XML/Prizes.xml"))
        Dim oXslDocument As New XslCompiledTransform()
        oXslDocument.Load(Server.MapPath("~/XSL/Prizes.xslt"))
        Dim _Stream As New System.IO.MemoryStream()
        Dim _stringBuilder As New System.Text.StringBuilder()
        Dim _stringWriter As New System.IO.StringWriter(_stringBuilder)

        Dim oXsltArgumentList As New XsltArgumentList()

        oXslDocument.Transform(oXmlDocument, oXsltArgumentList, _stringWriter)
        litPrizes.Text = _stringBuilder.ToString().Replace("~/", ResolveUrl("~/")).Replace("&lt;", "<").Replace("&gt;", ">").Replace("&#xD;", "").Replace("&#xA;", "").Replace("&quot;", "'")
    End Sub

    Protected Sub btnClaim_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClaim.Click
        If (Session("Username") IsNot Nothing) Then

            Dim _Points As New Points()
            _Points.Username = Session("Username").ToString()
            Dim _Profile As New BusinessLogic.Profile()
            _Profile.Username = Session("Username").ToString()
            _Profile.Load()
            _Points.Claim()
            Try
                ' create the email message 
                Dim _MailBody As String
                Dim oXmlDocument As New XmlDocument()
                oXmlDocument.Load(Server.MapPath("~/XML/PrizesMailBody.xml"))
                _MailBody = String.Format(oXmlDocument.InnerXml, _Profile.LastName, _Profile.FirstName, _Profile.Email, _Profile.Gender, _Profile.Address, _Profile.City, _Profile.State, _Profile.Zip, _Profile.BirthDay, _Profile.PhoneNumber)

                Dim message As New MailMessage()
                message.Body = _MailBody
                message.Subject = String.Format("Profile of {0} {1}", _Profile.LastName, _Profile.FirstName)
                message.To.Add(ConfigurationManager.AppSettings("ToMail"))
                ' create and add the attachment(s)
                'Dim attachment As New Attachment("sample.doc", MediaTypeNames.Application.Octet)
                'message.Attachments.Add(attachment)
                ' create SMTP Client and add credentials 
                Dim smtpClient As New SmtpClient()
                smtpClient.UseDefaultCredentials = True
                ' Email with Authentication 
                'smtpClient.Credentials = New Net.NetworkCredential("userID", "password", "domainName")
                'Send the message 
                smtpClient.Send(message)
            Catch ex As Exception

            End Try

        End If
    End Sub
End Class
